import axios from "axios";
import router from '@/router'
import { Toast } from 'vant'

const instance = axios.create({
    baseURL: 'https://geek.itheima.net',
    timeout:20000
  });
//请求拦截
instance.interceptors.request.use((config) => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = 'Bearer ' + token
    }
    return config
  }, (err) => {
    return Promise.reject(err)
  })
//响应拦截
instance.interceptors.response.use(({ data }) => {
    return data
  }, (err) => {
    if (err.response.status === 401) {
      Toast.fail('登录失效')
      router.push('/DengLu')
    }
    return Promise.reject(err)
  })
  
  export default instance